<template>
	<view class="pages">
		<view class="login">
			<view class="logo">
				<view class="bold">{{appConfig.platform_name||'商家平台名称'}}</view>
				<view class="m-t-14 bold">商家端登录</view>
			</view>
			<view class="acount-login">
				<view class="input">
					<u-field label-width="0" clear-size="38" icon="account" v-model="account" placeholder="请输入账号"
						:input-border="false" />
				</view>
				<view class="input">
					<u-field label-width="0" icon="lock-open" @right-icon-click="showPassword"
						:right-icon="isPassword?'eye':'eye-fill'" :password="isPassword" v-model="password"
						placeholder="请输入密码" :input-border="false" />
				</view>

				<view class="other-login flex-col flex-1">
					<view class="m-t-60 sm flex row-left">
						<u-checkbox activeColor="#40AFFA" v-model="checked" shape="circle"><text
								class="sm">已阅读并同意</text></u-checkbox>
						<router-link to="/bundle/pages/server_explan/server_explan?type=2">
							<view class="agreement primary">《隐私协议》</view>
						</router-link>
					</view>
				</view>

				<button class="login-btn white" size="lg" @tap="loginFun">登录</button>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		apiOrderList,
	} from '@/api/order'
	import tabBars from '../../utils/tabBar.js'
	import {
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex'
	import {
		apiAuthLogin,
		apiAccountLogin,
		apiLoginCaptcha,
		bindJpush
	} from '@/api/app';
	import wechath5 from '@/utils/wechath5'
	import {
		isWeixinClient,
		currentPage,
		client,
		trottle
	} from '@/utils/tools'
	import Cache from "@/utils/cache"
	import {
		BACK_URL,
		INVITE_CODE
	} from '@/config/cachekey'
	import {
		getWxCode,
		getUserProfile
	} from '@/utils/login'
	import store from 'store'
	import {
		initJPushService
	} from "@/utils/jPush.js"
	export default {
		data() {
			return {
				password: '',
				account: '',
				checked: false,
				isPassword: true,
				client: store.getters.client //1-微信小程序 2-微信公众号 3-安卓app 4-苹果app 5-pc端 6-h5
			};
		},

		methods: {
			...mapMutations(['login', 'tabBar/SET_TYPE']),
			...mapActions(['getUser']),
			// ...mapMutations(['tabBer/SET_TYPE']),
			getList(val, btype) {
				apiOrderList({
					goods_name: '',
					type: 'delivery',
					page_no: 1,
					page_size: 10,
				}).then(({
					list,
					size,
					more,
					count,
				}) => {
					// 重置数据
					console.log(tabBars)
					tabBars[btype][val].count = count
					uni.setStorage({
						key: 'tabBars',
						data: JSON.stringify(tabBars[btype])
					})
				})
			},
			// 账号登录
			async loginFun() {
				const {
					account,
					password,
					checked
				} = this
				if (!checked) return this.$toast({
					title: '请阅读并同意隐私协议'
				})

				if (!account) return this.$toast({
					title: '请输入账号'
				})

				if (!password) return this.$toast({
					title: '请输入密码'
				})

				apiAccountLogin({
					account,
					client: this.client,
					password: password
				}).then(res => {
					console.log('res', res)
					this.loginHandle(res)
				})
			},
			// 登录结果处理
			async loginHandle(data) {
				if (data.limit_menu.length == 0) {
					let type = 'adminList'
					// this['tabBar/SET_TYPE'](type)

					// tabBars[type][2].count = 15
					// uni.setStorage({
					// 	key: 'tabBars',
					// 	data: JSON.stringify(tabBars[type])
					// })
					this.getList(2, 'adminList')
					uni.setStorageSync('login_user_info', '[]');
				} else if (data.limit_menu[0] == '订单') {
					let type = 'orderList'
					// this['tabBar/SET_TYPE'](type)
					this.getList(0, 'orderList')
					// tabBars[type][0].count = 15
					// uni.setStorage({
					// 	key: 'tabBars',
					// 	data: JSON.stringify(tabBars[type])
					// })
					uni.setStorageSync('login_user_info', data.limit_menu);
				} else {}
				this.login(data)
				this.getUser()
				uni.hideLoading()
				uni.setStorageSync('isLoginBack', true)
				// #ifdef APP-PLUS
				const jpushModule = uni.requireNativePlugin("JG-JPush")
				jpushModule.getRegistrationID(result => {
					console.log('登陆当前设备的RegistrationID', result, result.registerID)
					bindJpush({
						registration_id: result.registerID
					}).then(res => {
						console.log('绑定RegistrationID到服务器结果', res)
						if (data.limit_menu.length == 0) {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						} else if (data.limit_menu[0] == '订单') {
							uni.reLaunch({
								url: '/pages/order_manage/order_manage'
							})
						}
					})
				})
				// #endif
			},

			showPassword() {
				this.isPassword = !this.isPassword
			}
		},

		onLoad() {
			const account = Cache.get('account')

			if (account) this.account = account

			this.getCodeUrl = trottle(this.getCodeUrl)
			// #ifdef H5
			// this.oaAutoLogin()
			// #endif
		},
		onUnload() {

		},
	};
</script>
<style lang="scss">
	page {
		background-color: #fff;

		padding: 0;
	}

	.login {
		height: 100vh;
		padding-top: 80rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		text-align: center;

		.logo {
			height: 126rpx;
			text-align: center;

			view:first-child {
				font-size: 52rpx;
			}

			view:last-child {
				font-size: 36rpx;
			}
		}


		.acount-login,
		.other-login {
			margin-top: 63rpx;
			width: calc(100% - 80rpx);

			.input {
				padding-top: 20rpx;
			}

			.login-btn {
				border-radius: 8rpx;
				margin: 30rpx 0 50rpx;
				background-color: $-color-primary;
			}
		}

		.agreement {
			color: $-color-primary;
		}
	}
</style>